<template>
    <div class="box">
        <!-- 导航栏 -->
        <van-nav-bar title="注册" left-text="" left-arrow @click-left="onClickLeft" />

        <!-- 表单 -->
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" placeholder="手机号"
                    :rules="[{ pattern: pattern, required: true, message: '请填写正确的手机号' }]" />
                <van-field v-model="password" :type="isShow ? 'text' : 'password'" name="password" label="密码"
                    placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]">
                    <template #button>
                        <van-icon name="eye-o" size="24" v-if="isShow" @click="isShow = !isShow" />
                        <van-icon name="closed-eye" size="24" v-else @click="isShow = !isShow" />
                    </template>
                </van-field>
                <van-field v-model="sms" center clearable label="短信验证码" placeholder="请输入验证码"
                    :rules="[{ required: true, message: '请输入短信验证码' }]">
                    <template #button>
                        <van-button size="small" type="primary" :disabled="time > 0 ? true : false"
                            @click="getCode()">{{ time > 0 ? time + '秒后重新获取验证码'
                                : '获取短信验证码' }}</van-button>
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    注册
                </van-button>
            </div>
        </van-form>
        <router-link to="/login">去登录</router-link>
    </div>
</template>

<script setup>
import { ref } from 'vue';

import { loginApi } from '@/api/api';

import { showSuccessToast, showFailToast } from 'vant';

const pattern = /^1[3-9]\d{9}$/;

const isShow = ref(true)

let sms = ref('')
let time = ref(0)
const getCode = () => {
    // console.log(Math.random().toString().slice(2,8))
    sms.value = Math.random().toString().slice(2, 8)
    time.value = 60

    let timer = setInterval(() => {
        time.value--;
        if (time.value == 0) {
            clearInterval(timer)
        }
    }, 1000);
}

import { useCounterStore } from '@/stores/counter'
// 在组件内部的任何地方均可以访问变量 `store` ✨
const store = useCounterStore()

import { useRoute, useRouter } from 'vue-router'
const router = useRouter()

const username = ref('');
const password = ref('');
const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value, password.value).then((res) => {
        console.log(res)
        if (res.data.code == 0) {
            showSuccessToast(res.data.msg);
            router.push("/login")
            // localStorage.setItem("token", res.data.data.token)
            // localStorage.setItem("username", res.data.data.username)

            store.getUserName(res.data.data.username)
        } else {
            showFailToast(res.data.msg);
        }
    })

};


const onClickLeft = () => history.back();

</script>

<style lang="scss" scoped>
.box {
    width: 100%;
    height: 100%;
}
</style>